package com.specialtreasure.freightdepot.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.app.core.util.XmlParser;

import com.specialtreasure.freightdepot.adapter.PWCityAdapter;
import com.specialtreasure.freightdepot.adapter.PWProvinceAdapter;
import com.specialtreasure.freightdepot.adapter.PWRegionAdapter;
import com.specialtreasure.freightdepot.model.Address;
import com.specialtreasure.freightdepot.model.SCity;
import com.specialtreasure.freightdepot.model.SProvince;
import com.specialtreasure.freightdepot.model.SRegion;

import org.xml.sax.SAXException;
import com.specialtreasure.freightdepot.R;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;



/**
 * Created by KUMA on 2017/7/25.
 * 省市县
 */

public class SelectCityPW extends PopupWindow {
    private Context mContext;
    private ViewPager vp_viewpager;
    private TextView tv_nowcity;
    private TextView tv_back;
    private TextView tv_back2;
    private TextView tv_cancel;

    private List<View> viewList;//view数组
    private List<SProvince> dataProvince;
    private List<SCity> dataCity;
    private List<SRegion> dataRegion;
    private PWProvinceAdapter adapterProvince;
    private PWCityAdapter adapterCity;
    private PWRegionAdapter adapterRegion;
    private Address mAddress;
    private OnResultListener mOnResultListener;

    public SelectCityPW(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    private void init() {
        mAddress = new Address();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.pw_city, null);
        vp_viewpager = (ViewPager) contentView.findViewById(R.id.vp_viewpager);
        tv_nowcity = (TextView) contentView.findViewById(R.id.tv_nowcity);
        tv_back = (TextView) contentView.findViewById(R.id.tv_back);
        tv_back2 = (TextView) contentView.findViewById(R.id.tv_back2);
        tv_cancel = (TextView) contentView.findViewById(R.id.tv_cancel);
        viewList = new ArrayList<>();// 将要分页显示的View装入数组中

        View view1 = inflater.inflate(R.layout.item_pwcity, null);
        GridView gridView1 = (GridView) view1.findViewById(R.id.gv_gridview);
        dataProvince = new ArrayList<>();
        try {
            List<SProvince> temp = XmlParser.parseCity(mContext);
            dataProvince.addAll(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        adapterProvince = new PWProvinceAdapter(mContext, dataProvince);
        gridView1.setAdapter(adapterProvince);
        viewList.add(view1);

        View view2 = inflater.inflate(R.layout.item_pwcity, null);
        GridView gridView2 = (GridView) view2.findViewById(R.id.gv_gridview);
        dataCity = new ArrayList<>();
        adapterCity = new PWCityAdapter(mContext, dataCity);
        gridView2.setAdapter(adapterCity);
        viewList.add(view2);

        View view3 = inflater.inflate(R.layout.item_pwcity, null);
        GridView gridView3 = (GridView) view3.findViewById(R.id.gv_gridview);
        dataRegion = new ArrayList<>();
        adapterRegion = new PWRegionAdapter(mContext, dataRegion);
        gridView3.setAdapter(adapterRegion);
        viewList.add(view3);

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_back.setVisibility(View.GONE);
                tv_back2.setVisibility(View.GONE);
                vp_viewpager.setCurrentItem(0);
                tv_nowcity.setText("请选择省份：");
            }
        });
        tv_back2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_back.setVisibility(View.VISIBLE);
                tv_back2.setVisibility(View.GONE);
                vp_viewpager.setCurrentItem(1);
                tv_nowcity.setText("当前所在城市：" + mAddress.province);
            }
        });

        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dataCity.clear();
                dataCity.addAll(dataProvince.get(position).cities);
                adapterCity.notifyDataSetChanged();

                tv_back.setVisibility(View.VISIBLE);
                tv_back2.setVisibility(View.GONE);

                //增加全国
                mAddress.province = dataProvince.get(position).name;
                if (dataProvince.get(position).name.equals("全国")){
                    if (mOnResultListener != null) {
                        mOnResultListener.onResult(mAddress);
                    }
                    SelectCityPW.this.dismiss();
                }else {
                    vp_viewpager.setCurrentItem(1);
                    tv_nowcity.setText("当前所在城市：" + dataProvince.get(position).name);
                }
            }
        });

        gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dataRegion.clear();
                dataRegion.addAll(dataCity.get(position).regions);
                adapterRegion.notifyDataSetChanged();
                vp_viewpager.setCurrentItem(2);
                tv_back.setVisibility(View.GONE);
                tv_back2.setVisibility(View.VISIBLE);
                tv_nowcity.setText("当前所在城市：" + dataCity.get(position).name);
                mAddress.city = dataCity.get(position).name;
            }
        });

        gridView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position != 0) {
                    mAddress.region = dataRegion.get(position).name;
                } else {
                    mAddress.region = "";
                }
                if (mOnResultListener != null) {
//                    if (mAddress.city.equals("全国")){
//                        mAddress.city="";
//                    }

                    mOnResultListener.onResult(mAddress);
                }
                SelectCityPW.this.dismiss();
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectCityPW.this.dismiss();
            }
        });

        vp_viewpager.setAdapter(pagerAdapter);

//        gv_gridview.setAdapter(adapter);
        // 设置SelectPicPopupWindow的View
        this.setContentView(contentView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
//        // 刷新状态
//        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }

    public void showAtBottom(View v) {
        this.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        backgroundAlpha(0.5f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        Activity activity = (Activity) mContext;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }

    PagerAdapter pagerAdapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return viewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            // TODO Auto-generated method stub
            container.removeView(viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            container.addView(viewList.get(position));


            return viewList.get(position);
        }
    };

    public interface OnResultListener {
        public void onResult(Address address);
    }

    public void setOnResultListener(OnResultListener OnResultListener) {
        this.mOnResultListener = OnResultListener;
    }
}
